using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Security;
using System.Web.UI;
using System.Data;
using System.Xml;

namespace QMWebParts
{
    public class QMEMExLogin: WebPart
    {
        String _qmwiseURL = "http://localhost/qmwise5/qmwise.asmx";
        String _linkText = "Login to EM";
        string _clientID = "Manager";
        string _checkSum = "434de524caad1f0bd4983c4cbf0cd0e9";

        QMWISeProxy.QMWISe service = new QMWebParts.QMWISeProxy.QMWISe();

        protected override void Render(HtmlTextWriter output)
        {            
            service.Url = _qmwiseURL;
            output.Write(BuildExLoginURL());            
        }

        private string BuildExLoginURL()
        {
            
            string _user = System.Web.HttpContext.Current.User.Identity.Name;
            string url = string.Empty;
            try
            {
                service.Url = _qmwiseURL;
                service.Security = new QMWebParts.QMWISeProxy.SecurityHeader();
                service.Security.ClientID = _clientID;
                service.Security.Checksum = _checkSum;
                url = service.GetAccessAdministrator(GetUserName());
                //url = service.GetAccessAssessmentList("Administrator");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "<BR><a href='" + url + "' target=_blank><B>" + _linkText + "</B></a><BR>";

        }

        private string GetUserName()
        {
            string _user = string.Empty;
            string[] username = System.Web.HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' });

            if (username.Length > 0)
                _user = username[username.Length - 1].ToString();
            else
                _user = System.Web.HttpContext.Current.User.Identity.Name;

            return _user;
        }


        [WebBrowsable(true), WebDescription("QMWISe URL"), WebDisplayName("QMWISe URL"), Personalizable(PersonalizationScope.User)]
        public string QMWISeURL
        {
            get { return _qmwiseURL; }
            set { _qmwiseURL = value; }

        }

        [WebBrowsable(true), WebDescription("Link Text"), WebDisplayName("Link Text"), Personalizable(PersonalizationScope.User)]
        public string LinkText
        {
            get { return _linkText; }
            set { _linkText = value; }

        }

        [WebBrowsable(true), WebDescription("Client ID"), WebDisplayName("Client ID"), Personalizable(PersonalizationScope.User)]
        public string Client_ID
        {
            get { return _clientID; }
            set { _clientID = value; }

        }

        [WebBrowsable(true), WebDescription("Check Sum"), WebDisplayName("Check Sum"), Personalizable(PersonalizationScope.User)]
        public string CheckSum
        {
            get { return _checkSum; }
            set { _checkSum = value; }

        }

    }
}
